limsFilemanagement.editPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.fileGridId,
            url: limsFilemanagement.baseUrl + '/queries',
            service: 'limsFilemanagementServiceImpl',
            dbTable: 'T_LIMS_FILEMANAGEMENT',
            columns: limsFilemanagement.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    // _this.refreshTabs(null)
                }
            },
            onRowActive: function (index, row) {
                // _this.refreshTabs(row)
            },
            deleteFormatter: function (row) {
                return row.fileName || row.fileCode || row.id
            },
            onCellClick: function (field, row) {
                field === 'fileCode' && limsFilemanagement.loadFileManagementDetailPage(row, 'edit')
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.FILEMANAGEMENTS.MODAL.ADD',
                        fields: limsFilemanagement.getBaseInfoFormFields(),
                        url: limsFilemanagement.baseUrl
                    }).done(function (id) {
                        limsFilemanagement.loadFileManagementDetailPage({id:id}, 'edit')
                        Gikam.getComp(_this.idManager.fileGridId).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(_this.idManager.fileGridId).deleteRows(limsFilemanagement.baseUrl)
                }
            }, {
                type: 'button',
                text: 'BASEMODULE.RM.FILEMANAGEMENTS.BUTTON.UPDATE_PERSON',
                icon: 'modify',
                onClick: function () {
                    _this.updatePerson()
                }
            }]
        }
    },

    updatePerson: function () {
        var _this = this
        var grid = Gikam.getComp(_this.idManager.fileGridId)
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM')
            return
        }
        var userIds = ''
        if (selections.length === 1) {
            userIds = selections[0].personId
        }
        Gikam.create('modal', {
            title: Gikam.choose.getConfig('mult-user').title,
            url: IFM_CONTEXT + Gikam.choose.getConfig('mult-user').url + Gikam.param({
                userIds: userIds,
                status_EQ: 'activated',
            }),
            onAfterClose: function (rows) {
                if (Gikam.isEmpty(rows)) {
                    return
                }
                let personId = rows.map(r => r.id).join(','), person = rows.map(r => r.userName).join(',')
                let data = {
                    personId: personId,
                    person: person
                }
                workspace.window.showMask()
                Gikam.postText(limsFilemanagement.baseUrl + '/update/person', Gikam.getJsonWrapper(data, [null, selections])).done(function () {
                    Gikam.alert('BASEMODULE.RM.FILEMANAGEMENTS.TIP.UPDATE_SUCCESS')
                    grid.refresh()
                }).always(function () {
                    workspace.window.closeMask()
                })
            }
        })
    },

    refreshTabs: function (row) {
        var _this = this
        Gikam.getAsyncComp(_this.idManager.fileVersionId).done(grid => {
            if (row) {
                grid.refresh({
                    url: limsFilemanagementVersion.baseUrl + '/queries',
                    requestData: {
                        fileId_EQ: row.id
                    }
                })
            } else {
                grid.refresh({
                    url: limsFilemanagementVersion.baseUrl + '/queries',
                    requestData: {
                        fileId_EQ: -1
                    }
                })
            }
        })
    },

    getFileVersionGrid: function (gridId) {
        var _this = this
        return {
            type: 'grid',
            id: gridId,
            // url: limsFilemanagementVersion.baseUrl + '/queries/raw',
            service: 'limsFilemanagementVersionServiceImpl',
            dbTable: 'T_LIMS_FILEMANAGEMENT_VERSION',
            columns: limsFilemanagementVersion.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                        uploader.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            deleteFormatter: function (row) {
                return row.versionCode || row.id
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                    uploader.setOptions({
                        bizId: row.id,
                    })
                })
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    var grid = Gikam.getComp(_this.idManager.fileGridId)
                    var actRow = grid.getActivedRow()
                    if (Gikam.isEmpty(actRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                        return
                    }
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.FILEMANAGEMENTS.MODAL.ADD',
                        fields: limsFilemanagementVersion.getBaseInfoFormFields(),
                        url: limsFilemanagementVersion.baseUrl,
                        param: {
                            fileId: actRow.id
                        },
                        titleWidth: 100,
                    }).done(function (id) {
                        Gikam.getComp(gridId).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(gridId).deleteRows(limsFilemanagementVersion.baseUrl)
                }
            }]
        }
    },

    getAttachmantStruct: function (gridId, dbTable) {
        return {
            type: 'uploader',
            immediateUpload: true,
            id: gridId + '-uploader',
            dbTable: dbTable,
            // bizId: -1,
            filter: false,
            genericQuery: false,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-filemanagement-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                title: 'BASEMODULE.RM.FILEMANAGEMENTS.TAB.FILEMANAGEMENT_ITEM',
                items: [this.getGridParam()]
            }
            /*            west: {
                            width: '35%',
                            title: 'BASEMODULE.RM.FILEMANAGEMENTS.TAB.FILEMANAGEMENT_ITEM',
                            items: [this.getGridParam()]
                        },
                        center: {
                            items: [{
                                type: 'layout',
                                north: {
                                    height: '55%',
                                    items: [_this.getFileVersionGrid(_this.idManager.fileVersionId)],
                                    title: 'BASEMODULE.RM.FILEMANAGEMENTS.TAB.FILEMANAGEMENTVERSION_ITEM',
                                },
                                center: {
                                    items: [_this.getAttachmantStruct(_this.idManager.fileVersionId, 'T_LIMS_FILEMANAGEMENT_VERSION')]
                                }
                            }]
                        }*/
        })
    },

    idManager: {
        fileGridId: 'lims-filemanagement-edit-list-grid',
        fileVersionId: 'lims-filemanagement-version-edit-list-grid',
    },

    init: function () {
        this.create()
    }
}
